// import Vue from 'vue'
import {createRouter, createWebHistory} from 'vue-router'
import Home from '@/views/home/Home.vue'
import updateAddress from '@/views/my/address/updateAddress.vue'
import shoeDetail from '@/views/shoe/shoeDetail/shoeDetail.vue'


import {getTokenLocal, getUserLocal} from "@/utils/auth.js"
import {showDialog} from "vant";
import * as Auth from "@/utils/auth.js";
import {tokenInfo} from "@/api/user.js";

let router = createRouter({
    // mode: 'history',
    history: createWebHistory('/qimo/'),
    routes: [
        //首页
        {
            path: '/',
            name: 'home',
            component: Home,
            meta: {
                title: "首页",
                requireAuth: true

            }
        },

        { //商品详情
            path: "/shoeDetail/:id",
            name: "shoeDetail",
            params: true,
            component: shoeDetail,
            meta: {
                title: "详情"
            }
        },
        { //分类
            path: "/classification",
            name: "classification",
            component: () => import('@/views/classification/classification.vue'),
            meta: {
                title: "分类",
                requireAuth: true

            }
        },
        { //登录
            path: "/login/:redirect",
            name: "login",
            params: true,
            component: () => import('@/views/my/login/login.vue'),
            meta: {
                title: "登录",
                requireAuth: true
            }
        },
        { //注册
            path: "/register",
            name: "register",
            component: () => import('@/views/my/login/register.vue'),
            meta: {
                title: "注册",
                requireAuth: true
            }
        },
        { //地址管理
            path: "/address",
            name: "address",
            component: () => import('@/views/my/address/addressList.vue'),
            meta: {
                title: "分类"
            }
        },
        { //修改地址
            path: "/updateAddress/:addressInfo/:addressId",
            name: "updateAddress",
            props: true,
            // query: {
            //     addressInfo: null,
            //     addressId: null,
            // },
            component: updateAddress,
            meta: {
                title: "修改地址"
            },

        },
        { //探索
            path: "/explore",
            name: "explore",
            component: () => import('@/views/explore/explore.vue'),
            meta: {
                title: "探索"

            },

        },
        { //我的
            path: "/user",
            name: "user",
            component: () => import('@/views/my/my.vue'),
            meta: {
                title: "我的",
                requireAuth: true
            }
        },
        { //列表
            path: "/shoeList/:title",
            name: "shoeList",
            component: () => import('@/views/shoe/shoeList/shoeList.vue'),
            meta: {
                title: "鞋子列表",
                requireAuth: true
            }
        },
        { //收藏列表
            path: "/collection",
            name: "collection",
            component: () => import('@/views/my/collection.vue'),
            meta: {
                title: "收藏列表"
            }
        },
        { //个人信息修改
            path: "/profile",
            name: "profile",
            component: () => import('@/views/my/profile.vue'),
            meta: {
                title: "个人信息修改"
            }
        },

        //工作中提示
        {
            path: "/working",
            component: () => import("@/views/Working.vue"),
            name: "working",
            meta: {
                title: "工作中...",
                requireAuth: true

            }
        },

        //不存在路由提示
        //要放在最后
        // {
        // 	path: "*",
        // 	name: "notFound",
        // 	component: () => import( /* webpackChunkName: "notfound" */ '@/views/Working.vue'),
        // 	meta:{
        // 		title:"找不到路由"
        // 	}
        // },

    ]
})

//设置全局路由导航守卫
router.beforeEach((to, from, next) => {

    // if (to.meta.title) {
    //     document.title = to.meta.title;
    // }

    if (to.name === 'login') { // 路由条状到login的，直接放行
        console.log(`${from.name}跳转到login`); // 控制台调试输出
        next(); // 路由继续往下跳转
        return;
    }

    if (to.meta.requireAuth) { // 路由不需要验证的，直接放行
        console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
        next();
        return;
    }

    // 路由需要验证的
    const token = getTokenLocal();
    if (!token) { // 如果token不存在的，则跳转登录login
        console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`);

        showDialog({message: "没有Token,需要重新登录吗?"},).then(() => {
            next({
                name: 'login',
                params: {
                    redirect: to.name
                }
            });
        }).catch(() => {
            router.push({
                name: "home"
            }).then(r => {
            });
        });
        return;
    }

    tokenInfo({token: token}).then(result => {
        console.log(result)
        if (result.data.data.isLogin === true) {
            console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`);
            next();
        } else {
            Auth.removeTokenLocal()
            Auth.removeUserLocal()
        }
    })
});

export default router